var router = require('koa-router')();
const tasks=require('../models/tasks');

router.prefix('/api/tasks');

router.post('/', function *(next) {
  const {text,user_id}=this.request.body;
  this.body = yield tasks.addTasks(text,user_id);
});

router.delete('/:id', function *(next) {
  const {id}=this.params;
  this.body = yield tasks.removeTaskById(id);
});

router.put('/:id', function *(next) {
  const {id}=this.params;
  const {text}=this.request.body;
  this.body = yield tasks.updateTaskById(text,id);
});

router.get('/', function *(next) {
  const {user_id}=this.query;
  const rows= yield tasks.getAllTasks(user_id);
  this.body = rows;
});

router.delete('/', function *(next) {
  const {user_id}=this.request.body;  
  this.body = yield tasks.deleteAllTasks(user_id);
});

module.exports = router;
